<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Hash;
//导入校验请求类
// use App\Http\Requests\LoginRequest;
class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    

    //登录页
    public function login(){
        return view("Home.Login.login");
    }
    public function dologin(Request $request){
        // dd($request->input());
        $user = DB::table('shop_user')
                            ->where('username', '=', $request->input('uname'))
                            ->first();
        if(!$user){
            return view("Home.Login.login", ['message'=>'账号不存在']);
        }

        if(Hash::check($request->password, $user->password)){
            //把用户信息上传session
            session(['uname' => $request->uname]);
            return redirect('/homeindex');
        }else{
            return view("Home.Login.login", ['message'=>'密码不正确']);
        }

    }

    //注册页
    public function register(){
        return view("Home.Login.register");
    }

    public function doregister(Request $request){
        // dd($request->input());
        $data                = $request->except('_token','repassword');
        $data['create_time'] = time();
        $data['password']    = Hash::make($data['password']);
        DB::beginTransaction(); //开启事务
        $add = DB::table('shop_user')
                        ->insert($data);
        if($add){
            DB::commit();  //提交
            return view("Home.Login.login", ['message'=>'注册成功,进行登录']);
        }else{
            DB::rollback();  //回滚
            return view("Home.Login.register", ['message'=>'注册失败']);
        }

        dd($data);
    }
}
